﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;using System.Configuration;
using System.Windows.Forms;

namespace ChairtyBank.DAL.PreAccept
{
    public class TamweelEntity
    {
        public Int32 T_Id { get; set; }
        public string T_Decission { get; set; }
        public string T_Reason { get; set; }
        public string T_Akhesaye { get; set; }
    }

    public class Tamweel
    {
        //Set absolute DB path rather than relative to solve openFileDialog reset path problem
        private static string ConnString = ConfigurationManager.ConnectionStrings["BankElKheir"].ToString();
        private static string CmdText = "";

        public static bool Add(TamweelEntity s)
        {
            bool flag = true;

            CmdText = "Insert into Tamweel (T_Id,T_Decission,T_Reason,T_Akhesaye) values (@T_Id,@T_Decission,@T_Reason,@T_Akhesaye)";

            SqlConnection Con = new SqlConnection(ConnString);
            SqlCommand cmd = new SqlCommand(CmdText, Con);

            cmd.Parameters.AddWithValue("@T_Id", s.T_Id);
            cmd.Parameters.AddWithValue("@T_Decission", s.T_Decission);
            cmd.Parameters.AddWithValue("@T_Reason", s.T_Reason);
            cmd.Parameters.AddWithValue("@T_Akhesaye", s.T_Akhesaye);

            try
            {
                Con.Open();
                int Num = cmd.ExecuteNonQuery();
                if (Num == 0)
                    flag = false;
            }

            catch (Exception ex)
            {
                MessageBox.Show("خطأ فى ادخال البيانات" + ex.Message);
                flag = false;
            }
            finally
            {
                if (Con.State == ConnectionState.Open)
                {
                    Con.Close();
                    flag = false;
                }
            }

            return flag;


        }

        public static bool Update(TamweelEntity s)
        {
            bool flag = true;

            CmdText = "Update Tamweel set T_Decission = @T_Decission , T_Reason =@T_Reason , T_Akhesaye = @T_Akhesaye";

            SqlConnection Con = new SqlConnection(ConnString);
            SqlCommand cmd = new SqlCommand(CmdText, Con);

            cmd.Parameters.AddWithValue("@T_Decission", s.T_Decission);
            cmd.Parameters.AddWithValue("@T_Reason", s.T_Reason);
            cmd.Parameters.AddWithValue("@T_Akhesaye", s.T_Akhesaye);


            try
            {
                Con.Open();
                int num = cmd.ExecuteNonQuery();
                if (num == 0)
                    flag = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show("خطأ فى تغيير البيانات " + ex.Message);
                flag = false;
            }
            finally
            {
                if (Con.State == ConnectionState.Open)
                {
                    Con.Close();
                    flag = false;
                }
            }

            return flag;

        }

        public static bool Delete(Int32 s)
        {
            bool flag = true;

            CmdText = "Delete from Tamweel where T_Id = @T_Id";

            SqlConnection Con = new SqlConnection(ConnString);
            SqlCommand cmd = new SqlCommand(CmdText, Con);

            cmd.Parameters.AddWithValue("@T_Id", s);

            try
            {
                Con.Open();
                int num = cmd.ExecuteNonQuery();
                if (num == 0)
                    flag = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show("خطأ اثناء الحذف ", "رساله ادارية " + ex.Message);
                flag = false;
            }
            finally
            {
                if (Con.State == ConnectionState.Open)
                {
                    Con.Close();
                    flag = false;
                }
            }

            return flag;
        }

        public static List<TamweelEntity> GetTamweelByID(Int32 id)
        {
            TamweelEntity x;
            List<TamweelEntity> T_List = new List<TamweelEntity>();

            CmdText = "Select * from Tamweel where T_Id = @T_Id";

            SqlConnection Con = new SqlConnection(ConnString);
            SqlCommand cmd = new SqlCommand(CmdText, Con);

            cmd.Parameters.AddWithValue("@T_Id", id);

            try
            {
                Con.Open();

                SqlDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    x = FillEntityFromReader(reader);
                    T_List.Add(x);
                }

                reader.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("خطأ عند تحديد البيانات المطلوبة من قاعدة البيانات " + ex.Message, "رساله ادارية ",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
            finally
            {
                if (Con.State == ConnectionState.Open)
                    Con.Close();
            }

            return T_List;
        }

        private static TamweelEntity FillEntityFromReader(SqlDataReader reader)
        {
            TamweelEntity x = new TamweelEntity();
            x.T_Id = Int32.Parse(reader["T_Id"].ToString());
            if (reader["T_Decission"] != null)
                x.T_Decission = reader["T_Decission"].ToString();
            if (reader["T_Reason"] != null)
                x.T_Reason = reader["T_Reason"].ToString();
            if ((reader["T_Akhesaye"] != null))
                x.T_Akhesaye = reader["T_Akhesaye"].ToString();

            return x;
        }

    }
}
